#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
安装图层保留功能所需的依赖包
"""

import subprocess
import sys
import os

def install_package(package_name):
    """安装Python包"""
    try:
        print(f"正在安装 {package_name}...")
        result = subprocess.run([
            sys.executable, '-m', 'pip', 'install', package_name
        ], capture_output=True, text=True, check=True)
        
        print(f"✅ {package_name} 安装成功")
        return True
        
    except subprocess.CalledProcessError as e:
        print(f"❌ {package_name} 安装失败:")
        print(f"错误输出: {e.stderr}")
        return False

def main():
    """主函数"""
    print("=" * 50)
    print("🔧 安装图层保留功能所需依赖")
    print("=" * 50)
    
    # 需要安装的包列表
    packages = [
        'tifffile==2023.9.26',
        'numpy>=1.21.0'
    ]
    
    success_count = 0
    total_count = len(packages)
    
    for package in packages:
        if install_package(package):
            success_count += 1
        print()
    
    print("=" * 50)
    print(f"安装完成: {success_count}/{total_count} 个包安装成功")
    
    if success_count == total_count:
        print("✅ 所有依赖安装成功！")
        print("\n🎉 图层保留功能现已可用:")
        print("   - 支持读取多层TIFF文件")
        print("   - 保留原始图层信息")
        print("   - JSON导入图片自动创建独立图层")
        print("   - 导出时可选择保留图层或合并图层")
        
        # 验证安装
        print("\n🔍 验证安装...")
        try:
            import tifffile
            import numpy as np
            print("✅ tifffile 导入成功")
            print("✅ numpy 导入成功")
            print(f"   tifffile 版本: {tifffile.__version__}")
            print(f"   numpy 版本: {np.__version__}")
        except ImportError as e:
            print(f"❌ 导入验证失败: {e}")
    else:
        print("❌ 部分依赖安装失败，请手动安装:")
        print("   pip install tifffile==2023.9.26 numpy>=1.21.0")
    
    print("\n📝 使用说明:")
    print("   1. 重启Flask应用程序")
    print("   2. 导入包含图层的TIF文件作为底板")
    print("   3. 在导出时选择TIF格式并勾选'保留图层'")
    print("   4. 导出的TIF文件将保留所有图层信息")

if __name__ == '__main__':
    main()
